package cn.blog.controller;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.blog.common.LayuiTableResult;
import cn.blog.common.PublicResultJosn;
import cn.blog.entity.PlayerUser;
import cn.blog.service.PlayerUserService;

/**
 * 比赛选手管理
 * 
 * @author An-Il
 *
 */
@RestController
@RequestMapping("/palyerUser")
public class PlayerUserController {

	@Autowired
	private PlayerUserService playerUserService;

	/**
	 * 添加一条选手信息
	 * 
	 * @param playerUser
	 * @return
	 */
	@PostMapping
	public PublicResultJosn add(@RequestBody PlayerUser playerUser) {
		PublicResultJosn resultJosn = playerUserService.add(playerUser);
		return resultJosn;
	}

	/**
	 * 单、批量删除一条比赛信息
	 * 
	 * @param ids
	 * @return
	 */
	@DeleteMapping
	public PublicResultJosn delete(@RequestBody List<String> ids) {
		// 校验
		if (CollectionUtils.isEmpty(ids)) {
			throw new IllegalArgumentException("删除id参数不能为空");
		}
		PublicResultJosn resultJosn = playerUserService.delete(ids);
		return resultJosn;
	}

	/**
	 * 更新选手信息
	 * 
	 * @param playerUser
	 * @return
	 */
	@PutMapping
	public PublicResultJosn update(@RequestBody PlayerUser playerUser) {
		// 校验
		if (StringUtils.isBlank(playerUser.getPuid())) {
			throw new IllegalArgumentException("id参数异常");
		}
		PublicResultJosn resultJosn = playerUserService.update(playerUser);
		return resultJosn;
	}

	/**
	 * 模糊分页查询选手信息
	 * 
	 * @param playerUser
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@GetMapping
	public LayuiTableResult select(PlayerUser playerUser, @RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize) {
		LayuiTableResult tableResult = playerUserService.query(playerUser, page, pageSize);
		return tableResult;
	}

	/**
	 * 根据id查询选手基本信息
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/{id}")
	public PublicResultJosn selectById(@PathVariable String id) {
		// 校验
		if (StringUtils.isBlank(id)) {
			throw new IllegalArgumentException("id参数异常");
		}
		PublicResultJosn resultJosn = playerUserService.queryOne(id);
		return resultJosn;
	}
}
